package com.soso.base.search.service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.soso.base.util.log.Logger;

public abstract class AbstractSearchService {
	protected String html;
	protected int endPosition;
	public AbstractSearchService(){
	}
	public AbstractSearchService(String html,String areaStartString,String areaEndString){
		this.html=initHtml(html,areaStartString,areaEndString);
	}
	
	private String initHtml(String html,String startString,String endString){
		try{
			String regex="(.*)";
			if(startString!=null&&!"".equals(startString)
					&&endString!=null&&!"".equals(endString)){
				startString=startString.replaceAll("(?<!\\\\)\\(", "(?:");//替换为不捕获到组
				regex=startString+"(.*?)"+endString;
			}else if(startString!=null&&!"".equals(startString)){
				startString=startString.replaceAll("(?<!\\\\)\\(", "(?:");//替换为不捕获到组
				regex=startString+"(.*)";
			}else if(endString!=null&&!"".equals(endString)){
				regex="(.*)"+endString;
			}else{
				return html;
			}
			Pattern p = Pattern.compile(regex,Pattern.DOTALL);
			Matcher m = p.matcher(html);
			if(m.find()) {
				return m.group(1);
			}
		}catch(Exception e){
			Logger.error(e.getMessage());
		}
		return html;
	}
	protected final void offset(String content, String searchRegex, int startPosition,int options){
		endPosition=-1;
		Matcher matcher = Pattern.compile(searchRegex,options).matcher(content);
		if(matcher.find(startPosition)) {
			endPosition=matcher.end();
		}
	}
	/**
	 * HTML标签嵌套匹配
	 * @param searchRegex
	 * @param content
	 * @param startPosition
	 * @param options
	 * @return
	 */
	protected final String regexSearch(String content, String searchRegex, int startPosition,int options){
		endPosition=-1;
		Matcher matcher = Pattern.compile("(<([a-zA-Z]+)\\s*.*?>)\\(?\\.\\*\\??\\)?(</\\2>)",Pattern.DOTALL).matcher(searchRegex);
		if(!matcher.find()) {
			String result="";
			matcher = Pattern.compile(searchRegex,options).matcher(content);
			if(matcher.find(startPosition)) {
				if(matcher.groupCount()>0){
					result=matcher.group(1);
				}else{
					result=matcher.group();
				}
				endPosition=matcher.end();
			}
			return result;
		}
		
		String startStr=matcher.group(1);
		String label=matcher.group(2);
		String endStr=matcher.group(3);
		
		String regex=startStr;
		matcher = Pattern.compile(regex,options).matcher(content);
		if(!matcher.find(startPosition)) {
			return "";
		}
		int beginIndex=matcher.end();
		startPosition=beginIndex;
		
		regex="(.*?)(<[/]?"+label+".*?>)";
		matcher = Pattern.compile(regex,options).matcher(content);
		
		int count=0;
		while(matcher.find(startPosition)){
			endPosition=matcher.end();
			startPosition=endPosition;
			String endLabel=matcher.group(2);
			if(endLabel.equals(endStr)){
				if(count==0){
					//嵌套匹配完成
					int endIndex=endPosition-endStr.length();
					return content.substring(beginIndex, endIndex);
				}
				count--;//出栈
				continue;
			}
			count++;//入栈
		}
		//嵌套匹配失败，HTML不完整。
		return "";
	}
	
	protected String getRegex(String startString,String endString){
		if(startString==null||"".equals(startString)||endString==null||"".equals(endString)){
			return null;
		}
		String regex="(.*)";
		if(startString!=null&&!"".equals(startString)
				&&endString!=null&&!"".equals(endString)){
			startString=startString.replaceAll("(?<!\\\\)\\(", "(?:");//替换为不捕获到组
			regex=startString+"(.*?)"+endString;
		}else if(startString!=null&&!"".equals(startString)){
			startString=startString.replaceAll("(?<!\\\\)\\(", "(?:");//替换为不捕获到组
			regex=startString+"(.*)";
		}else if(endString!=null&&!"".equals(endString)){
			regex="(.*)"+endString;
		}
		return regex;
	}
}
